﻿using System;
using System.Collections.Generic;
using System.Text;

namespace WFL.Utils
{
    public static class ClipboardFormatHelper
    {
        public static string Html(string htmlText, string sourceURL = null, string version = "0.9")
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("Version:");
            sb.Append(version);
            sb.Append("\r\nStartHTML:0000000000\r\nEndHTML:0000000000\r\nStartFragment:0000000000\r\nEndFragment:0000000000\r\n");
            if (!string.IsNullOrEmpty(sourceURL))
            {
                sb.Append("SourceURL:");
                sb.Append(sourceURL);
                sb.Append("\r\n");
            }
            int startHtml = Encoding.UTF8.GetByteCount(sb.ToString());
            sb.Append("<html>\r\n<body>\r\n<!--StartFragment-->");
            int startFragment = Encoding.UTF8.GetByteCount(sb.ToString());
            sb.Append(htmlText);
            int endFragment = Encoding.UTF8.GetByteCount(sb.ToString());
            sb.Append("<!--EndFragment-->\r\n</body>\r\n</html>");
            int endHtml = Encoding.UTF8.GetByteCount(sb.ToString());
            //替换描述参数
            //字符偏移量，数组索引从0开始，减掉1
            int offset = 8 + version.Length + 2 - 1;
            //StartHTML
            string numStr = startHtml.ToString();
            offset = offset + 20 - numStr.Length + 1;
            for (int i = 0; i < numStr.Length; i++)
            {
                sb[offset + i] = numStr[i];
            }
            offset += numStr.Length - 1 + 2;
            //EndHTML
            numStr = endHtml.ToString();
            offset = offset + 18 - numStr.Length + 1;
            for (int i = 0; i < numStr.Length; i++)
            {
                sb[offset + i] = numStr[i];
            }
            offset += numStr.Length - 1 + 2;
            //StartFragment
            numStr = startFragment.ToString();
            offset = offset + 24 - numStr.Length + 1;
            for (int i = 0; i < numStr.Length; i++)
            {
                sb[offset + i] = numStr[i];
            }
            offset += numStr.Length - 1 + 2;
            //EndFragment
            numStr = endFragment.ToString();
            offset = offset + 22 - numStr.Length + 1;
            for (int i = 0; i < numStr.Length; i++)
            {
                sb[offset + i] = numStr[i];
            }
            return sb.ToString();
        }
    }
}
